////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity.groups.more;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import cc.telecomdigital.tdstock.BaseAsyncTask;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.BaseActivity;
import cc.telecomdigital.tdstock.activity.groups.MoreGroup;
import cc.telecomdigital.tdstock.adapter.CommonListItemAdapter;
import cc.telecomdigital.tdstock.adapter.CommonViewHolder;

/**
 * @author haibo
 */
public class DXnewsListActivity extends BaseActivity
{
	protected static final String			TAG			= DXnewsListActivity.class.getSimpleName();
	private static final long				START_ID	= 90000l;

	private ListView						_listView;
	private Button							_toolBtnReturn;

	private TextView						_topBar;

	private static CommonListItemAdapter	_newsAdapter;
	private static List<CommonViewHolder>	_newsList;
	private String[]						_newsTitleList;
	private String[]						_newsIdList;
	private String[]						_vendor;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dx_news);

		_toolBtnReturn = (Button) findViewById(R.id.more_returnbtn);
		_toolBtnReturn.setFocusable(true);
		_toolBtnReturn.setClickable(true);
		_toolBtnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				TDStockLog.d(TAG, "close current activity and forwarding to the previous");

				Intent intent = new Intent(getApplicationContext(), MoreActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				SwitchForwardActivity(MoreActivity.class, intent);
			}
		});

		_messageHandler = null; // this activity no need it.
		_listView = (ListView) findViewById(R.id.more_dx_news_list);

		_newsList = new ArrayList<CommonViewHolder>();

		_topBar = (TextView) findViewById(R.id.top_bar_title);
		_topBar.setText(getResources().getStringArray(R.array.toolbuttons_more_list)[1]);
	}

	private class FetchNewsCategories extends BaseAsyncTask
	{
		public FetchNewsCategories(Context context)
		{
			super(context);
		}

		@Override
		protected Void doInBackground(String... params)
		{
			_newsTitleList = new String[] { "港股快訊", "外匯速遞", "大行報告" };
			_newsIdList = new String[] { "0", "9", "8" };
			_vendor = new String[] { "D", "X", "D" };
			return null;
		}

		// can use UI thread here
		@Override
		protected void onPostExecute(Void result)
		{
			super.onPostExecute(result);
			updateAdapter();
		}
	}

	private void updateAdapter()
	{
		if (!_newsList.isEmpty())
			_newsList.clear();

		long id = START_ID + _newsList.size();
		for (int index = 0, listSize = _newsTitleList == null ? 0
				: _newsTitleList.length; index < listSize; index++)
		{
			id += index;
			CommonViewHolder viewHolder = new CommonViewHolder();
			viewHolder.id = id;
			viewHolder.text = new TextView(this);
			viewHolder.text.setText(_newsTitleList[index]);
			viewHolder.sign = new ImageView(this);
			viewHolder.vendor = _vendor[index];
			_newsList.add(viewHolder);
		}

		_listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id)
			{
				if (position != -1)
				{
					FinetListItemsActivity._fromActivityClass = DXnewsListActivity.class;

					FinetListItemsActivity._title = _newsTitleList[position];
					FinetListItemsActivity._catId = _newsIdList[position];
					FinetListItemsActivity._vendorId = _vendor[position];

					Intent intent = new Intent(getApplicationContext(), FinetListItemsActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
					SwitchForwardActivity(FinetListItemsActivity.class, intent);
				}
			}
		});

		if (_listView.getAdapter() == null || _listView.getAdapter().getCount() < 1)
		{
			_newsAdapter = new CommonListItemAdapter(LayoutInflater.from(this), _newsList);
			_listView.setAdapter(_newsAdapter);
		}
		else
		{
			_newsAdapter.notifyDataSetChanged();
		}
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		if (_listView.getAdapter() == null || _listView.getAdapter().getCount() < 1)
		{
			new FetchNewsCategories(getMyOwnContext()).execute(); 
		}
		_listView.invalidateViews();
	}

	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		return MoreGroup.GetActivityGroupHostControl();
	}

}
